﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace cebao.Web.info
{
    /// <summary>
    /// GetMinfo 的摘要说明
    /// </summary>
    public class GetMinfo : IHttpHandler, IRequiresSessionState
    {
        cebao.BLL.mobileSendSearchInfo bll = new cebao.BLL.mobileSendSearchInfo();
        cebao.BLL.mobileReciveSearchInfo bllr = new BLL.mobileReciveSearchInfo();
        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);
            else if (ac == "getinfo")
                getinfo(context);
            else if (ac == "add")
                add(context);
            else if (ac == "pic")
                getpic(context);
            else if (ac == "delete")
                delete(context);
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["pid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                if (bll.DeleteAll(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
        }
        public void getpic(HttpContext context)
        {
            context.Response.ContentType = "image/jpg";
            int pid = 0;
            string pidstr = "";
            if (null != context.Request.QueryString["mid"])
            {
                pidstr = context.Request.QueryString["mid"].ToString().Trim();
                pid = int.Parse(pidstr);
            }
            byte[] p = bll.GetPic(pid);
            context.Response.BinaryWrite(p);//返回给前台页面  
            context.Response.End();


        }
        public void getinfo(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            //===============================================================  
            //获取查询条件:【用户id,开始时间，结束时间，关键字】  
            int pid = 0;
            string pidstr = "", psinfo = "";
            //获取前台传来的值 
            if (null != context.Request.QueryString["iid"])
            {//获取前台传来的值  
                pidstr = context.Request.QueryString["iid"].ToString().Trim();
                pid = int.Parse(pidstr);
            }
            DataSet ds = bllr.GetList("mobileSendSearchInfoId =" + pid);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //psinfo += "<div class='info'>" + ds.Tables[0].Rows[i]["reciveInfo"].ToString() + "</div><div class='suser'>" + ds.Tables[0].Rows[i]["utruename"].ToString() +"<span class='ftime'>"+ ds.Tables[0].Rows[i]["reciveTime"].ToString() + "</span></div>";
                psinfo += " <div class='liuyan' style='margin-top:5px;'><div  class='liuyantop'><font color='#0e2d5f'>" + ds.Tables[0].Rows[i]["utruename"].ToString() + "回复</font>&nbsp;&nbsp;发表于：" + ds.Tables[0].Rows[i]["reciveTime"].ToString() + "</div><div class='liuyancontent'>" + ds.Tables[0].Rows[i]["reciveInfo"].ToString() + "</div></div>";
            }


            context.Response.Write(psinfo);//返回给前台页面  
            context.Response.End();


        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                //string title = context.Request.Form[""];
                string caidstr = context.Request.Form["iid"];
                int caid = 0;
                caid = int.Parse(caidstr);
                string uidstr = context.Request.Form["uid"];
                int uaid = 0;
                uaid = int.Parse(uidstr);
                string infocontent = context.Request.Form["infocontent"];
                cebao.Model.mobileReciveSearchInfo model = new cebao.Model.mobileReciveSearchInfo();
                model.reciveInfo = infocontent;
                model.ruid = uaid;
                model.suid = uid;
                model.isAuto = false;
                model.mobileSendSearchInfoId = caid;
                int rid = bllr.Add(model);
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }

        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string uname, utruename, uprovince, ucity, uunit, ucounty, utype = "1";
            utruename = uname = uprovince = ucity = uunit = ucounty = "";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                StringBuilder strWhere = new StringBuilder();
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (ucounty != "")
                {
                    strWhere.AppendFormat(" ucounty='{0}' and ", ucounty);
                }

                if (ucity != "")
                {
                    strWhere.AppendFormat(" ucity='{0}' and ", ucity);
                }

                if (uprovince != "")
                {
                    strWhere.AppendFormat(" uprovince='{0}' and ", uprovince);

                }
                if (uname != "")
                {
                    strWhere.AppendFormat(" searchInfo like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", uname);
                }

                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}